import { createModel } from "hox";
import { useState } from "react";
import { ResponseTypes, useGetRequest } from "../http";
import { getDashboardInfo, getD60MortgageRate, getD90MortgageRate } from "../http/apis/dashboard-api";
import useLoginModel from "./useLoginModel";

interface DashboardInfo {
    everyMonthAmts: number
    months: Array<string>
    overDueRepayAmt: string
    custNum: string
    leftoverAsset: string
    yesterdayAmt: string
    yearAmt: string
}

interface DInfo {
    D60MortgageRate: string
    D90MortgageRate: string
    status: boolean
}

export interface UseDashboard {
    startGetDashboardInfo: () => Promise<ResponseTypes>
    dashboardInfo: DashboardInfo | undefined
    startGetD60Info: () => Promise<ResponseTypes>
    d60Info: DInfo | undefined
    startGetD90Info: () => Promise<ResponseTypes>
    d90Info: DInfo | undefined
}

const useDashboard = () => {

    const userInfo = useLoginModel()

    const [dashboardInfo, setDashboardInfo] = useState<DashboardInfo>()
    const [d60Info, setD60Info] = useState<DInfo>()
    const [d90Info, setD90Info] = useState<DInfo>()

    const dashboardParams = { url: getDashboardInfo, successAction: (data: ResponseTypes) => setDashboardInfo(data.content) }
    const getDashboardInfoRequest = useGetRequest(dashboardParams)
    const startGetDashboardInfo = () => getDashboardInfoRequest.run(null)
    const d60Params = { url: getD60MortgageRate, successAction: (data: ResponseTypes) => setD60Info(data.content) }
    const getD60Request = useGetRequest(d60Params)
    const startGetD60Info = () => getD60Request.run({ custManageId: userInfo.user?.username })
    const d90Params = { url: getD90MortgageRate, successAction: (data: ResponseTypes) => setD90Info(data.content) }
    const getD90Request = useGetRequest(d90Params)
    const startGetD90Info = () => getD90Request.run({ custManageId: userInfo.user?.username })

    return {
        startGetDashboardInfo,
        dashboardInfo,
        startGetD60Info,
        d60Info,
        startGetD90Info,
        d90Info
    }
}

export default createModel(useDashboard)